package capsolver_go

import (
	"context"
	"gitee.com/kaylee595/gkaylee/kerr"
	"github.com/mitchellh/mapstructure"
)

type ReqCreateTaskGeetest struct {
	Type       TaskType `json:"type" mapstructure:"type"`                               // required
	WebsiteURL string   `json:"websiteURL" mapstructure:"websiteURL"`                   // required
	GT         string   `json:"gt,omitempty" mapstructure:"gt,omitempty"`               // Only Geetest V3 is required
	Challenge  string   `json:"challenge,omitempty" mapstructure:"challenge,omitempty"` // Only Geetest V3 is required
	CaptchaId  string   `json:"captchaId,omitempty" mapstructure:"captchaId,omitempty"` // only Geetest V4 is required
	// GeetestApiServerSubdomain optional, Special api subdomain, example: api.geetest.com
	GeetestApiServerSubdomain string `json:"geetestApiServerSubdomain,omitempty" mapstructure:"geetestApiServerSubdomain,omitempty"`
}

// CreateTaskGeetest https://docs.capsolver.com/en/guide/captcha/Geetest/
func (s *Solver) CreateTaskGeetest(ctx context.Context, i ReqCreateTaskGeetest) (string, error) {
	task := make(Task)
	err := mapstructure.Decode(i, &task)
	if err != nil {
		return "", kerr.WrapTraceStack(err)
	}
	retCreateTask, err := s.CreateTask(ctx, task)
	if err != nil {
		return "", kerr.WrapTraceStack(err)
	}
	return retCreateTask.TaskId, nil
}

type RespGetTaskResultGeetest struct {
	// GeetestV3
	Challenge string `json:"challenge" mapstructure:"challenge"`
	Validate  string `json:"validate" mapstructure:"validate"`

	// GeetestV4
	CaptchaId     string `json:"captcha_id" mapstructure:"captcha_id"`
	CaptchaOutput string `json:"captcha_output" mapstructure:"captcha_output"`
	GenTime       string `json:"gen_time" mapstructure:"gen_time"`
	LotNumber     string `json:"lot_number" mapstructure:"lot_number"`
	PassToken     string `json:"pass_token" mapstructure:"pass_token"`
	RiskType      string `json:"risk_type" mapstructure:"risk_type"`
}

// GetTaskResultGeetest https://docs.capsolver.com/en/guide/captcha/Geetest/
func (s *Solver) GetTaskResultGeetest(ctx context.Context, taskId string) (*RespGetTaskResult[RespGetTaskResultGeetest], error) {
	retCreateTask, err := s.GetTaskResult(ctx, taskId)
	if err != nil {
		return nil, kerr.WrapTraceStack(err)
	}
	ret := new(RespGetTaskResult[RespGetTaskResultGeetest])
	ret.TaskId = retCreateTask.TaskId
	ret.Status = retCreateTask.Status
	err = mapstructure.Decode(retCreateTask.Solution, &ret.Solution)
	if err != nil {
		return nil, kerr.WrapTraceStack(err)
	}
	return ret, nil
}
